package spring.boot.btc.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

@Slf4j
@Service
public class BlockSenderService {

    public static final String HOST = "224.0.0.1";
    public static final int PORT = 1234;

    public void send(String message) {
        MulticastSocket ms = null;
        try {
            InetAddress ip = InetAddress.getByName(HOST);
            DatagramPacket packet = new DatagramPacket(message.getBytes(), message.length(), ip, PORT);
            ms = new MulticastSocket();
            ms.send(packet);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (ms != null) {
                ms.close();
            }
        }
    }

}
